package summary;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/13 15:13
 * https://leetcode-cn.com/problems/binary-tree-paths/
 */
public class Title257 {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static List<String> binaryTreePaths(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        List<String> result = new ArrayList<>();
        Stack<String> paths = new Stack<>();
        stack.push(root);
        paths.push(String.valueOf(root.val));
        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            String path = paths.pop();
            if (pop.left == null && pop.right == null) {
                result.add(path);
            }
            if (pop.right != null) {
                stack.add(pop.right);
                paths.push(path + "->" + pop.right.val);
            }
            if (pop.left != null) {
                stack.add(pop.left);
                paths.push(path + "->" + pop.left.val);
            }
        }
        return result;
    }



    public List<String> binaryTreePaths1(TreeNode root) {
        List<String> result = new ArrayList<>();
        LinkedList<TreeNode> list = new LinkedList<>();
        if (root == null) {
            return result;
        }
        LinkedList<String> paths = new LinkedList<>();
        list.offer(root);
        paths.offer(String.valueOf(root.val));
        while (!list.isEmpty()) {
            int size = list.size();
            for (int i = 0; i < size; i++) {
                TreeNode pop = list.poll();
                String poll = paths.poll();
                if (pop.left == null && pop.right == null) {
                    result.add(poll);
                }
                if (pop.left != null) {
                    list.offer(pop.left);
                    paths.offer(poll + "->" + pop.left.val);
                }
                if (pop.right != null) {
                    list.offer(pop.right);
                    paths.offer(poll + "->" + pop.right.val);
                }
            }
        }
        return result;
    }


    public List<String> binaryTreePaths3(TreeNode root) {
        List<String> paths = new ArrayList<>();
        if (root == null) {
            return paths;
        }
        dfs(root, "", paths);
        return paths;
    }

    private void dfs(TreeNode root, String path, List<String> paths) {
        if (root == null) {
            return;
        }
        path += root.val;
        if (root.left == null && root.right == null) {
            paths.add(path);
            return;
        }
        dfs(root.left, path + "<", paths);
        dfs(root.right, path + "<", paths);
    }


    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.right = new TreeNode(5);
        binaryTreePaths(root);
    }
}
